﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;
using DevComponents.Editors.DateTimeAdv;
using DevComponents.Editors;

namespace RM2012Server.Controller
{
    public class BookingCtrl
    {
        BookingDAO m_BookingDAO = new BookingDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "BookId";
            cmb.ValueMember = "BookId";
        }

        public void LoadToComboBoxWithCustomer(ComboBoxEx cmb)
        {
            cmb.DataSource = GetWithCustomer();
            cmb.DisplayMember = "FullName";
            cmb.ValueMember = "CusId";
        }

        public void LoadToComboBoxCompleted(ComboBoxEx cmb)
        {
            cmb.DataSource = GetWithCustomerCompleted();
            cmb.DisplayMember = "FullName";
            cmb.ValueMember = "CusId";
        }

        public void LoadToComboBoxUnCompleted(ComboBoxEx cmb)
        {
            cmb.DataSource = GetWithCustomerUnCompleted();
            cmb.DisplayMember = "FullName";
            cmb.ValueMember = "CusId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "BookId";
            cmb.ValueMember = "BookId";
            cmb.DataPropertyName = "BookId";
            cmb.HeaderText = "Đặt hàng";
        }

        public void LoadToDataGridViewComboBoxColumnState(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = BookingStateSource();
            cmb.DisplayMember = "StateName";
            cmb.ValueMember = "State";
            cmb.DataPropertyName = "State";
            cmb.HeaderText = "Trạng thái";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "BookId";
            cmb.ValueMember = "BookId";
            cmb.DataPropertyName = "BookId";
            cmb.HeaderText = "Đặt hàng";
        }

        public List<BookingStateInfo> BookingStateSource()
        {
            List<BookingStateInfo> list = new List<BookingStateInfo>();
            list.Add(new BookingStateInfo(0, "Đợi phục vụ"));
            list.Add(new BookingStateInfo(1, "Khách không đến"));
            list.Add(new BookingStateInfo(2, "Đã phục vụ"));
            return list;
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtBookId,
                         ComboBoxEx cmbCusId,
                         TextBoxX txtBookTime,
                         DateTimeInput dtBookTime,
                         TextBoxX txtTablesId,
                         CheckBoxX chkHaveTables,
                         ComboBoxEx cmbTablesId,
                         IntegerInput iiQuantity,
                         ComboBoxEx cmbState,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtBookId.DataBindings.Clear();
            txtBookId.DataBindings.Add("Text", bs, "BookId");

            cmbCusId.DataBindings.Clear();
            cmbCusId.DataBindings.Add("SelectedValue", bs, "CusId");

            txtBookTime.DataBindings.Clear();
            txtBookTime.DataBindings.Add("Text", bs, "BookTime");

            if (txtBookTime.Text != "" && txtBookTime.Text != null)
            {
                dtBookTime.DataBindings.Clear();
                dtBookTime.DataBindings.Add("Value", bs, "BookTime");
            }

            txtTablesId.DataBindings.Clear();
            txtTablesId.DataBindings.Add("Text", bs, "TablesId");
            cmbTablesId.DataBindings.Clear();
            cmbTablesId.DataBindings.Add("SelectedValue", bs, "TablesId");

            iiQuantity.DataBindings.Clear();
            iiQuantity.DataBindings.Add("Value", bs, "Quantity");

            cmbState.DataBindings.Clear();
            cmbState.DataBindings.Add("SelectedIndex", bs, "State");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }


        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtBookId,
                         ComboBoxEx cmbCusId,
                         TextBoxX txtBookTime,
                         DateTimeInput dtBookTime,
                         TextBoxX txtTablesId,
                         CheckBoxX chkHaveTables,
                         ComboBoxEx cmbTablesId,
                         IntegerInput iiQuantity,
                         ComboBoxEx cmbState,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            txtBookId.DataBindings.Clear();
            txtBookId.DataBindings.Add("Text", bs, "BookId");

            cmbCusId.DataBindings.Clear();
            cmbCusId.DataBindings.Add("SelectedValue", bs, "CusId");

            txtBookTime.DataBindings.Clear();
            txtBookTime.DataBindings.Add("Text", bs, "BookTime");

            if (txtBookTime.Text != "" && txtBookTime.Text != null)
            {
                dtBookTime.DataBindings.Clear();
                dtBookTime.DataBindings.Add("Value", bs, "BookTime");
            }

            txtTablesId.DataBindings.Clear();
            txtTablesId.DataBindings.Add("Text", bs, "TablesId");
            cmbTablesId.DataBindings.Clear();
            cmbTablesId.DataBindings.Add("SelectedValue", bs, "TablesId");

            iiQuantity.DataBindings.Clear();
            iiQuantity.DataBindings.Add("Value", bs, "Quantity");

            cmbState.DataBindings.Clear();
            cmbState.DataBindings.Add("SelectedIndex", bs, "State");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }
        
        public DataTable GetAll()
        {
            return m_BookingDAO.GetAll();
        }

        public DataTable GetById(String bookId)
        {
            return m_BookingDAO.GetById(bookId);
        }

        public DataTable GetByLikeId(String bookId)
        {
            return m_BookingDAO.GetByLikeId(bookId);
        }

        public DataTable GetWithCustomer()
        {
            return m_BookingDAO.GetWithCustomer();
        }

        public DataTable GetWithCustomerCompleted()
        {
            return m_BookingDAO.GetWithCustomerCompleted();
        }

        public DataTable GetWithCustomerUnCompleted()
        {
            return m_BookingDAO.GetWithCustomerUnCompleted();
        }

        public int Add(BookingInfo info)
        {
            return m_BookingDAO.Add(info);
        }

        public int Update(BookingInfo info)
        {
            return m_BookingDAO.Update(info);
        }

        public int Delete(String bookId)
        {
            return m_BookingDAO.Delete(bookId);
        }

        public DataRow AddNewRow()
        {
            return m_BookingDAO.AddNewRow();
        }

        public void AddNewBooking(DataRow m_Row)
        {
            m_BookingDAO.AddNewBooking(m_Row);
        }

        public bool SaveData()
        {
            return m_BookingDAO.SaveData();
        }
    }
}
